package com.muki.simple.repository;

import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;

import com.muki.simple.entity.Content;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

@Repository
public class ContentPipeline implements Pipeline {

	@Autowired private ContentRepository contentRepository;
	
	@Override
	public void process(ResultItems resultItems, Task task) {
		for(Map.Entry<String, Object> entry:resultItems.getAll().entrySet()) {
			if(entry.getKey().equals("content")) {
				Content content = (Content) entry.getValue();
                if(contentRepository.
                		findByLink(content.getLink())==null) {
                	contentRepository.save(content);
                }
			}
		}
	}

}
